﻿using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using Yitter.IdGenerator;
using Furion.DatabaseAccessor;
namespace Admin.NET.Application;
/// <summary>
/// 考试服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class MyKaoshiService : IDynamicApiController, ITransient
{
    private readonly UserManager _userManager;
    private readonly SqlSugarRepository<Kaoshi> _rep;
    private readonly SqlSugarRepository<KaoshiXueke> _kaoshiXuekeRep;
    private readonly SqlSugarRepository<Shichang> _shichangRep;
    private readonly SqlSugarRepository<Kaosheng> _kaoshengRep;
    private readonly SqlSugarRepository<Banji> _banjiRep;
    private readonly SqlSugarRepository<JiaoxueBan> _jiaoxueBanRep;
    private readonly SqlSugarRepository<Student> _studentRep;
    public MyKaoshiService(SqlSugarRepository<Kaoshi> rep, SqlSugarRepository<KaoshiXueke> kaoshiXuekeRep, SqlSugarRepository<Shichang> shichangRep, SqlSugarRepository<Kaosheng> kaoshengRep, SqlSugarRepository<Banji> banjiRep, SqlSugarRepository<JiaoxueBan> jiaoxueBanRep, SqlSugarRepository<Student> studentRep, UserManager userManager)
    {
        _rep = rep;
        _kaoshiXuekeRep = kaoshiXuekeRep;
        _shichangRep = shichangRep;
        _kaoshengRep = kaoshengRep;
        _banjiRep = banjiRep;
        _jiaoxueBanRep = jiaoxueBanRep;
        _studentRep = studentRep;
        _userManager = userManager;
    }

    /// <summary>
    /// 分页查询考试
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<KaoshengOutput>> Page(KaoshiInput input)
    {
        var username = _userManager.Account;
        var student = await _studentRep.AsQueryable().Where(p => p.Xh == username).FirstAsync();
        if(student == null)
        {
            return null;
        }
        var res = await _kaoshengRep.AsQueryable()
            .Where(p => p.StudentId == student.Id)
            .LeftJoin<Kaoshi>((u, kaoshiid) => u.KaoshiId == kaoshiid.Id)
            .LeftJoin<Banji>((u, kaoshiid, banjiid) => u.BanjiId == banjiid.Id)
            .LeftJoin<Student>((u, kaoshiid, banjiid, studentid) => u.StudentId == studentid.Id)
            .LeftJoin<JiaoxueBan>((u, kaoshiid, banjiid, studentid, jiaoxueBanid) => u.JiaoxueBanId == jiaoxueBanid.Id)
            .Select((u, kaoshiid, banjiid, studentid, jiaoxueBanid) => new KaoshengOutput
            {
                Id = u.Id,
                KaoshiId = u.KaoshiId,
                KaoshiIdName = kaoshiid.Name,
                BanjiId = u.BanjiId,
                BanjiIdName = banjiid.Name,
                JiaoxueBanId = u.JiaoxueBanId,
                JiaoxueBanIdName = jiaoxueBanid.Name,
                StudentId = u.StudentId,
                StudentIdName = studentid.Name,
                StudentIdXh = studentid.Xh,
                KaoHao = u.KaoHao,
                ShiChangNum = u.ShiChangNum,
                ZuoweiHao = u.ZuoweiHao,
                ZhunkaoZhenghao = u.ZhunkaoZhenghao,
                Active = u.Active,
                ZongFen = u.ZongFen,
                BanjiMC = u.BanjiMC,
                JiMC = u.JiMC,
                CreateUserName = u.CreateUserName,
                UpdateUserName = u.UpdateUserName,
            }).ToPagedListAsync(input.Page, input.PageSize);
        foreach (var item in res.Items)
        {
            var shichang = await _shichangRep.AsQueryable().Where(p => p.KaoshiId == item.KaoshiId && p.ShiChangNum == item.ShiChangNum).FirstAsync();
            item.Address1 = shichang.Address1;
            item.Address2 = shichang.Address2;
        }
        return res;
    }
}

